<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <link rel="stylesheet" href="../../lib/jq-module/zyupload/zyupload-1.0.0.min.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }

    </style>
</head>
<body>
<div class="layui-form layuimini-form" lay-filter="role-form">
    <div class="layui-form-item">
        <label class="layui-form-label required">角色名称</label>
        <div class="layui-input-block">
            <input type="text" name="roleName" lay-verify="required" lay-reqtext="角色名称不能为空" placeholder="请填写角色名称" value="" class="layui-input">
            <input type="text" name="roleId" value="" hidden>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">权限字符</label>
        <div class="layui-input-block">
            <input type="text" name="roleKey" lay-verify="required" lay-reqtext="角色键名不能为空" placeholder="请填写角色键名" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">显示顺序</label>
        <div class="layui-input-block">
            <input type="text" name="roleSort" placeholder="" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <select name="status">
                <option value="0">正常</option>
                <option value="1">停用</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">备注信息</label>
        <div class="layui-input-block">
            <textarea name="remark" class="layui-textarea" placeholder="请填写备注信息"></textarea>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">菜单权限</label>
        <div class="layui-input-block">
            <div id="menuIds"></div>
            <input type="text" name="menuIds" value="" hidden>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认修改</button>
        </div>
    </div>
</div>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../../lib/common/common.js" charset="utf-8"></script>
<script src="../../lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script>

    window.onload=function(){
        let roleId = GetQueryString("roleId");
        if(isNotNull(roleId)){
            init(roleId);
            initLayModule(roleId);
        }else{
            var iframeIndex = parent.layer.getFrameIndex(window.name);
            parent.layer.close(iframeIndex);
        }
    }

    /**
     * 初始化数据
     */
    function init(roleId) {
        $.ajax({
            type:"get",
            url:'/role/get/'+roleId,
            dataType:'json',
            contentType: 'application/x-www-form-urlencoded',
            success:function(req){
                layui.use(['form'], function () {
                    let form = layui.form
                    form.val('role-form',{
                        "roleId": req.data.roleId,
                        "roleName": req.data.roleName,
                        "roleKey": req.data.roleKey,
                        "roleSort": req.data.roleSort,
                        "status": req.data.status,
                        "menuIds": req.data.menuIds,
                        "remark": req.data.remark
                    });
                })
            },
            error:function(e){
                layer.msg("数据有误");
            }
        })
    }

    function initLayModule(roleId){
        layui.use(['form','tree'], function () {
            var form = layui.form,
                tree = layui.tree;
            layer = layui.layer,
                $ = layui.$;

            //初始化树形菜单
            localStorage.setItem('checkChild',"false");//初始化去掉全选功能
            tree.render({
                elem: '#menuIds'  //绑定元素
                ,id: 'menuTreeId'
                ,showCheckbox: true
                ,checkChild: false
                ,data: getTreeData(roleId)
            });
            localStorage.setItem('checkChild',"true");//初始化页面完成后恢复全选功能

            //监听提交
            form.on('submit(saveBtn)', function (data) {
                var index = layer.alert('确定提交修改吗？', {
                    title: '提示'
                }, function () {
                    let checkData = tree.getChecked('menuTreeId');
                    let menuIds_val = [];
                    let checkDataStr = getChecked_list(checkData);
                    let checkDataArr = checkDataStr.split(",");
                    for(let i in checkDataArr){
                        menuIds_val.push(checkDataArr[i]);
                    }
                    data.field.menuIds = menuIds_val
                    let myMsg = layer.msg("正在提交...",{
                        icon:16,
                        time:-1
                    })
                    $.ajax({
                        type:"post",
                        url:'/role/edit',
                        data:JSON.stringify(data.field),
                        dataType:'json',
                        contentType: 'application/json; charset=UTF-8',
                        success:function(req){
                            layer.msg("修改成功");
                            layer.close(index);
                            // 关闭弹出层
                            setTimeout(function () {
                                layer.close(myMsg);//手动关闭
                                let iframeIndex = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(iframeIndex);
                            }, 1500);
                        },
                        error:function(e){
                            layer.msg("修改失败");
                            layer.close(myMsg);//手动关闭
                        }
                    })
                });

                return false;
            });

        });
    }

    function getTreeData(roleId) {
        let data;
        let obj = {roleId: roleId};
        $.ajax({
            type:"post",
            url:'/menu/roleMenuTreeData',
            data:JSON.stringify(obj),
            dataType:'json',
            contentType: 'application/json; charset=UTF-8',
            async: false,
            success:function(req){
                data = req.data
            },
            error:function(e){
                alert("出错啦！")
            }
        })
        return data;
    }

    // 获取选中节点的id
    function getChecked_list(data) {
        var id = "";
        $.each(data, function (index, item) {
            if (id != "") {
                id = id + "," + item.id;
            }
            else {
                id = item.id;
            }
            var i = getChecked_list(item.children);
            if (i != "") {
                id = id + "," + i;
            }
        });
        return id;
    }

</script>
</body>
</html>